<?php

class Application_Model_Language
{

    protected static $_default_locale = 'ru';
    protected static $_current_locale = false;
    protected static $_language_list  = false;

    protected $_id;
    protected $_key;
    protected $_name;
    protected $_is_default;

    public function setId($value)
    {
        $this->_id = $value;
    }

    public function getId()
    {
        return $this->_id;
    }

    public function setKey($value)
    {
        $this->_key = $value;
    }

    public function getKey()
    {
        return $this->_key;
    }

    public function setName($value)
    {
        $this->_name = $value;
    }

    public function getName()
    {
        return $this->_name;
    }


    public function setIsDefault($value)
    {
        $this->_is_default = $value;
    }

    public function getIsDefault()
    {
        return $this->_is_default;
    }

    /**
     * determineCurrent
     *
     * This function is used to determine current language.
     */
    public static function determineCurrent()
    {
        $locale    = self::$_default_locale;
        $languages = self::getList();

        foreach ($languages as $language)
        {
            if ($language->getIsDefault())
            {
                $locale = $language->getKey();
                break;
            }
        }
        self::setCurrent($locale);
    }

    /**
     * setCurrent
     *
     * This function is used to set current language.
     */
    public static function setCurrent($locale)
    {
        self::$_current_locale = $locale;
        require_once 'Zend/Translate.php';
        $translator = new Zend_Translate(
            'Array',
            APPLICATION_PATH . '/locale/',
            $locale,
            array(
                'scan' => Zend_Translate::LOCALE_FILENAME
            )
        );
        Zend_Registry::set ('Zend_Translate', $translator);

        require_once 'Zend/Log/Writer/Stream.php';
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH  .'/logs/translate.log');
        require_once 'Zend/Log.php';
        $log    = new Zend_Log($writer);
        $translator->setOptions(array(
            'log'             => $log,
            'logMessage'      => "Unknownn message '%message%' in '%locale%'",
            'logUntranslated' => true
        ));
    }

    /**
     * getList
     *
     * This function returns list of currently available languages represented as Application_Model_Language instances.
     */
    public static function getList()
    {
        if (!empty(self::$_language_list) && is_array(self::$_language_list))
        {
            return self::$_language_list;
        }

        $language_mapper      = new Application_Model_LanguageMapper();
        self::$_language_list = $language_mapper->fetchAll();
        return self::$_language_list;
    }

    /**
     * getCurrent
     *
     * This function returns currently used Application_Model_Language object.
     */
    public static function getCurrent()
    {
        $languages = self::getList();
        $result    = false;

        if (is_array($languages))
        {
            foreach ($languages as $language)
            {
                if ($language->getKey() == self::$_current_locale)
                {
                    return $language;
                }
            }
        }

        return $result;
    }

}